package com.hspedu.tankgame04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener,Runnable {
    //定义我的坦克
    HeroTank hero1 = null;

    //定义敌人坦克，多线程用Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTankeSize = 3;

    //定义Vector，用于存放炸弹
    //当子弹击中坦克时，加入一个Bomb对象到bombs
    Vector<Bomb> bombs = new Vector<>();
    //定义三张图片，用于显示爆炸效果
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public MyPanel() {
        //初始化我的坦克
        hero1 = new HeroTank(500, 100);
        //初始化敌人坦克
        for (int i = 0; i < enemyTankeSize; i++) {
            //创建一个敌人坦克
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0);
            //设置方向
            enemyTank.setDirect(2);
            //启动敌人坦克线程
            new Thread(enemyTank).start();
            //给该enemyTank加入一颗子弹
            Shot shot = new Shot(enemyTank.getX()+20, enemyTank.getY()+60, enemyTank.getDirect());
            //加入enemyTank的Vector成员
            enemyTank.shots.add(shot);
            //启动shot对象
            new Thread(shot).start();
            //加入
            enemyTanks.add(enemyTank);


        }
        //初始化图片对象
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bao1.png"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bao2.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bao3.png"));
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        g.fillRect(0,0,1000,750);
        // 判断自己的坦克是否存活
        if (hero1 != null && hero1.isLive) {
            //画自己的坦克
            drawTank(hero1.getX(), hero1.getY(), g, hero1.getDirect(),1);
            //画hero的子弹;画出heroTank的子弹集合
            for (int j=0; j<hero1.shots.size(); j++){
                //取出子弹
                Shot shot = hero1.shots.get(j);
                //绘制
                if (shot != null && shot.isLive) {
                    drawShot(shot.x, shot.y, g, 1); //画子弹
                } else { //移除
                    hero1.shots.remove(shot);
                }
            }
        }
        //如果bombs中有对象，就画出
        for (int i = 0; i < bombs.size(); i++) {
            //取出炸弹
            Bomb bomb = bombs.get(i);
            //根据当前这个bomb对象的life值去画出对应的图片
            if(bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }else if(bomb.life > 3){
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            }else{
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }
            //让炸弹生命值减少
            bomb.lifeDown();
            //bomb生命值为0，从bombs中删除
            if(bomb.life == 0){
                bombs.remove(bomb);
            }
        }
        //画敌人的坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            //取出坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            //判断坦克是否还存活
            if(enemyTank.isLive){
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
                //画出enemyTank所有子弹
                for (int j=0; j<enemyTank.shots.size(); j++){
                    //取出子弹
                    Shot shot = enemyTank.shots.get(j);
                    //绘制
                    if (shot.isLive) {
                        drawShot(shot.x, shot.y, g, 0); //画子弹
                    } else { //移除
                        enemyTank.shots.remove(shot);
                    }
                }
            }
        }
    }

    /**
     * @param x 坦克的左上角x坐标
     * @param y 坦克的左上角y坐标
     * @param g 画笔
     * @param direct 坦克的方向
     * @param type 坦克类型
     * */
    public void drawTank(int x, int y, Graphics g, int direct, int type){
        switch (type){
            case 0: //我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向来绘制坦克
        switch (direct){
            case 0: //向上
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x+30, y, 10, 60, false);
                g.fill3DRect(x+10, y+10, 20, 40, false);
                g.fillOval(x+10, y+20, 20, 20);
                g.drawLine(x+20, y+30, x+20, y);
                break;
            case 1: //向右
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y+30, 60, 10, false);
                g.fill3DRect(x+10, y+10, 40, 20, false);
                g.fillOval(x+20, y+10, 20, 20);
                g.drawLine(x+30, y+20, x+60, y+20);
                break;
            case 2: //向下
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x+30, y, 10, 60, false);
                g.fill3DRect(x+10, y+10, 20, 40, false);
                g.fillOval(x+10, y+20, 20, 20);
                g.drawLine(x+20, y+30, x+20, y+60);
                break;
            case 3: //向左
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y+30, 60, 10, false);
                g.fill3DRect(x+10, y+10, 40, 20, false);
                g.fillOval(x+20, y+10, 20, 20);
                g.drawLine(x+30, y+20, x, y+20);
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    public void drawShot(int x, int y, Graphics g, int type){
        switch (type){
            case 0: //我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        g.fill3DRect(x, y, 5, 5, false);
    }

    //判断子弹是否击中坦克，在run()方法中判断
    public void hitTank(Shot s, Tank enemyTank){
        //判断击中坦克
        switch (enemyTank.getDirect()){
            case 0: //坦克向上
            case 2: //坦克向下
                if(s.x>enemyTank.getX() && s.x<enemyTank.getX()+40
                    && s.y>enemyTank.getY() && s.y<enemyTank.getY()+60){
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //创建Bomb对象，加入到bombs集合
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                    //击中后，从集合中删除
                    enemyTanks.remove(enemyTank);
                }
                break;
            case 1: //坦克向右
            case 3: //坦克向左
                if(s.x>enemyTank.getX() && s.x<enemyTank.getX()+60
                        && s.y>enemyTank.getY() && s.y<enemyTank.getY()+40){
                    s.isLive = false;
                    enemyTank.isLive = false;
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                    enemyTanks.remove(enemyTank);
                }
                break;
        }
    }

    //多颗子弹是否击中敌人坦克
    public void hitEnemyTank() {
        for (int j=0; j < hero1.shots.size();j++) {
            Shot shot = hero1.shots.get(j);
            if (shot != null && shot.isLive) {
                //遍历敌人所有坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    hitTank(shot, enemyTank);
                }
            }
        }
    }

    //多颗子弹是否击中我方坦克
    public void hitHeroTank() {
        for (int i=0; i < enemyTanks.size();i++) {
            //取出敌人坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            //遍历enemyTank对象的所有子弹
            for (int j=0; j < enemyTank.shots.size();j++) {
                //取出子弹
                Shot shot = enemyTank.shots.get(j);
                //判断shot是否击中我的坦克
                if (hero1.isLive && shot.isLive){
                    hitTank(shot, hero1);
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //移动
        if(e.getKeyCode() == KeyEvent.VK_W){
            hero1.setDirect(0);
            hero1.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            hero1.setDirect(1);
            hero1.moveRight();
        }else if (e.getKeyCode() == KeyEvent.VK_S){
            hero1.setDirect(2);
            hero1.moveDown();
        }else if (e.getKeyCode() == KeyEvent.VK_A){
            hero1.setDirect(3);
            hero1.moveLeft();
        }


        //射击
        if (e.getKeyCode() == KeyEvent.VK_J){
            //发射子弹
            hero1.shotEnemyTank();
        }
        //this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {  //每隔100毫秒，重绘区域
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            hitEnemyTank(); //判断是否击中了敌人坦克
            hitHeroTank(); //判断敌人是否击中我的坦克
            this.repaint();
        }
    }
}
